from fastapi import APIRouter, Depends
from starlette.responses import FileResponse

from apps.functional import cruds
from apps.functional import schemas
from apps.functional.params.testcase import TestcaseParams, TestcaseVersion
from core.auth import Auth, FullAdminAuth
from core.dependencies import IdList
from core.response import SuccessResponse
from utils.file.file_base import FileBase

router = APIRouter(prefix="/testcase")


@router.post("/list", summary="获取测试用例列表")
async def list_testcase(
        data: schemas.TestcaseCatalogs,
        params: TestcaseParams = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    datas, count = await cruds.TestcaseDal(auth.db).list_testcases(data, **params.dict())
    return SuccessResponse(datas, count=count)


@router.post("/create", summary="创建用例")
async def create_testcase(
        data: schemas.Testcase,
        auth: Auth = Depends(FullAdminAuth())
):
    data.create_user_id = auth.user.id
    data.update_user_id = auth.user.id
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).create_data(data=data),
        msg="创建成功"
    )


@router.get("/info/{data_id}", summary="获取用例详情")
async def get_testcase_info(
        data_id: int,
        params: TestcaseVersion = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).testcase_info(data_id=data_id, **params.dict())
    )


@router.put("/update/{data_id}", summary="更新测试用例")
async def update_testcase(
        data_id: int,
        data: schemas.Testcase,
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).update_testcase(data_id, data),
        msg="修改成功"
    )


@router.get("/versions/{data_id}", summary="获取测试用例的历史版本")
async def get_testcase_history_versions(
        data_id: int,
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).testcase_versions(data_id)
    )


@router.post("/change_version", summary="切换用例版本")
async def change_testcase_version(
        data: schemas.ChangeTestcaseVersion,
        auth: Auth = Depends(FullAdminAuth())
):
    await cruds.TestcaseDal(auth.db).change_testcase_version(data)
    return SuccessResponse(
        msg="版本切换成功"
    )


@router.post("/new_version", summary="创建新版本用例")
async def new_testcase_version(
        data: schemas.NewTestcaseVersion,
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).new_testcase_version(data),
        msg="版本新增成功"
    )


@router.delete("/delete_version", summary="删除用例版本")
async def delete_testcase_version(
        data: schemas.TestcaseVersion,
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).delete_testcase_version(data),
        msg="版本删除成功"
    )


@router.delete("/delete", summary="删除用例")
async def delete_testcase(
        ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(
        await cruds.TestcaseDal(auth.db).delete_datas(ids.ids, v_soft=True),
        msg="删除成功"
    )


@router.get("/import/template", summary="下载导入模板")
async def down_import_template():
    abs_path = await FileBase.get_abs_file("static", "templates", "测试用例导入模板.xlsx")
    return FileResponse(abs_path, filename="测试用例导入模板.xlsx")
